//
//  ZZReweetedStatusView.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/17.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit
import SnapKit

class ZZReweetedStatusView: UIView {

    var bottomConstraints: Constraint?
    //重写模型的set
    var retweetedStatus: ZZStatus? {
        didSet{
            retweetedLabel.text = retweetedStatus?.text
            
//            pictureView.imageURLs = retweetedStatus?.imageURLs
            
            //移除约束
            self.bottomConstraints?.uninstall()
            
            if let picurls = retweetedStatus?.imageURLs where picurls.count > 0{
                
                //给图片赋值
                pictureView.imageURLs = picurls
                
                //显示图片
                pictureView.hidden = false
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            }else {
                //图片隐藏
                pictureView.hidden = true
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
            
        }
    }
    
    // MARK: 重构
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 设置UI
    private func setUpUi() {
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    }
    
    // MARK: 懒加载控件
    //正文
    private lazy var retweetedLabel: UILabel = UILabel(title: "大牛", size: 14, color: UIColor.darkGrayColor(), alphaNum: 1, margin: StatusCellMargin)
    //配图
    private lazy var pictureView: ZZStatusPictureView = ZZStatusPictureView()
}







